<#include "/WEB-INF/views/admin/common/layout/__formlayout.html">
<#assign pageCss>
<link rel="stylesheet" href="${ctx}/resource/validationengine/css/validationEngine.jquery.css">
</#assign>
<#assign pageScript>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine-en.js"></script>
<script type="text/javascript" src="${ctx}/resource/validationengine/js/jquery.validationEngine.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$("#user_id").change(function() {
		var selected = $("#user_id option:selected").text();
		if(selected == 'please select agent'){
			$("#user_name").val("");
		}else{
			$("#user_name").val(selected);
		}
		
		
	});
	
	
	$('#user_id').chosen();
	$("#frm").validationEngine('attach', {
		promptPosition : 'topLeft',
		scroll : false
	});
	$("#subBtn").click(function() {
		var flag = true;
		var agent = $("#user_id").val();
		var offerId = $("#offer_id").val();
		if(agent=='0' &&  offerId==''){
			flag = false;
			layer.msg("agent or offerId must choose one", {
				time : 1000,
				icon : 2
			});
		}
		
		if(agent!='0' &&  offerId !=''){
			flag = false;
			layer.msg("agent and offerId, you can select only one", {
				time : 1000,
				icon : 2
			});
		}
		
		var frm = $("#frm");
		if (frm.validationEngine("validate") && flag) {
			 $("#user_id").attr("disabled",false);
			 $("#offer_id").attr("disabled",false);
			$.ajax({
				type : "POST",
				url : "${ctx}/admin/sys/deduct/save_update",
				data : frm.serialize(),
				success : function(result) {
					if (result.isSuccess) {
						layer.msg(result.msg, {
							icon : 1,
							time : 1000,
							end : function() {
								//信息提示完成后关闭表单窗口
								var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
								parent.layer.close(index); //再执行关闭  
							}
						});
					} else {
						layer.msg(result.msg, {
							time : 1000,
							icon : 2
						});
					}
				}
			});
		}
	});
});
</script>
</#assign>
<@formlayout>
<div class="row">
	<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
		<div class="widget am-cf">
			<div class="widget-body am-fr">
				<form class="am-form tpl-form-border-form" id="frm"
					action="">
					<input type="hidden" name="deduction.id" id="id" value="${deduct.id!}">
					<input type="hidden" name="deduction.user_name" id="user_name" value="${deduct.user_name!}">
					<div class="am-form-group">
						<label for="user_id" class="am-u-sm-3 am-form-label">agent </label>
					<div class="am-u-sm-9">
								<#if deduct??>
									<select name="deduction.user_id" id="user_id" disabled="disabled">
										<option value="0">please select agent</option>
										<#list users as agent>
											<#if deduct.userId?? && deduct.userId == agent.id>
												<option value="${agent.id}" selected="selected">${agent.name}</option>
											<#else>
												<option value="${agent.id}">${agent.name}</option>
											</#if>
										</#list>
									</select>
								<#else>
								<select name="deduction.user_id" id="user_id">
										<option value="0">please select agent</option>
										<#list users as agent>
											<#if deduct.userId?? && deduct.userId == agent.id>
												<option value="${agent.id}" selected="selected">${agent.name}</option>
											<#else>
												<option value="${agent.id}">${agent.name}</option>
											</#if>
										</#list>
									</select>
								</#if>
							</div>
					</div>
					<div class="am-form-group">
						<label for="offer_id" class="am-u-sm-3 am-form-label">offerId</label>
						<div class="am-u-sm-9">
							<#if deduct??>
							<input type="text" disabled="disabled"
								class="tpl-form-input validate[custom[number]]" id="offer_id" value="${deduct.offerId!}"
								name="deduction.offer_id" placeholder="please enter offerId">
							<#else>
							<input type="text" 
								class="tpl-form-input validate[custom[number]]" id="offer_id" value="${deduct.offerId!}"
								name="deduction.offer_id" placeholder="please enter offerId">
							</#if>
						</div>
					</div>
					
					<div class="am-form-group">
						<label for="ratio" class="am-u-sm-3 am-form-label">ratio</label>
						<div class="am-u-sm-9">
							<input type="text"
								class="tpl-form-input validate[required,custom[number],max[100],min[0]]" id="ratio" value="${deduct.ratio!}"
								name="deduction.ratio" placeholder="please enter ratio">
						</div>
					</div>
					<div class="am-form-group">
						<div class="am-u-sm-9 am-u-sm-push-3">
							<button type="button"
								class="am-btn am-btn-primary tpl-btn-bg-color-success "
								id="subBtn">submit</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>
</@formlayout>

